package com.quarantine.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.quarantine.model.announcement;
import com.quarantine.service.AnnouncementManager;
import com.quarantine.util.DateUtils;
import com.quarantine.web.CrudActionSupport;

@Namespace("/admin")
@Results({
	@Result(name ="save", location = "announcement-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"}),
	@Result(name ="delete", location = "announcement-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"})
})
public class AnnouncementAdminAction extends CrudActionSupport<announcement> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private announcement entity;
	private AnnouncementManager announcement;
	private String id;
	private List<announcement> announcementList;
	private String ids;// 逗号隔开id集合
	private String time ;
	/**
	 * 分頁用
	 */
	private int currentpage;
	private int pageNo;
	public static final int pageSize = 20;
	private int prePage;
	private long totalPages;
	private int nextPage;
	private long allsize;
	
    
	@Override
	public announcement getModel() {
		// TODO Auto-generated method stub
		return null;
	}
/**
 * 列出公告列表 
 */
	@Override
	public String list() throws Exception {
		int pageSize = 9;
		Page<announcement> page = new Page<announcement>();
		page.setAutoCount(true);
		page.setPageNo(currentpage);
		page.setPageSize(pageSize);
	    announcement.getList(page);
		setAnnouncementList(page.getResult());
		pageNo = page.getPageNo();
		prePage = page.getPrePage();
		nextPage = page.getNextPage();
		totalPages = page.getTotalPages();
		if(totalPages==0){
			
			pageNo=0;
		}
		allsize = page.getTotalCount();
		return "list";
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		if(id!=null&&id!="")
		 {
			 ;
		 }
		else{
			 
				entity.setClickrate(0);
			 
		}


		
		
		/*entity.setEditTime(DateUtils.now());*/
		 
		 announcement.save(entity);
		return "save";
	}

	@Override
	public String delete() throws Exception {
		announcement.delete(id);
		return "delete";
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new announcement();
		} else {
			entity = announcement.getById(id);
		}
		
	}
	/**
	 * 按id批量删除
	 */
	public String deleteByIds() {
          for (String id : ids.split(";")) 
          {
	        entity = announcement.getById(id);
	       
	        announcement.delete(id);
	       }
         return "delete";
	}
	public String show() {
		entity = announcement.getById(id);
		return "addnews";
	}	
	public String show1() {
		 time=DateUtils.now();
		return "addnews";
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getCurrentpage() {
		return currentpage;
	}
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPrePage() {
		return prePage;
	}
	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}
	public long getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}
	public int getNextPage() {
		return nextPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public long getAllsize() {
		return allsize;
	}
	public void setAllsize(long allsize) {
		this.allsize = allsize;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public announcement getEntity() {
		return entity;
	}

	public void setEntity(announcement entity) {
		this.entity = entity;
	}

	public AnnouncementManager getAnnouncement() {
		return announcement;
	}
	@Autowired
	public void setAnnouncement(AnnouncementManager announcement) {
		this.announcement = announcement;
	}
	public List<announcement> getAnnouncementList() {
		return announcementList;
	}
	public void setAnnouncementList(List<announcement> announcementList) {
		this.announcementList = announcementList;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}

}
